package com.china08.yxyapi.api.manage.tec.teaching.resource.myresource;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.es.ElasticYxyResResource;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.service.yxyresource.YxyResourceService;
import com.china08.yxyapi.util.EsResourceUpdateUtil;

import io.swagger.annotations.ApiOperation;

/**
 * 老师 我的资源 修改资源
 * 
 */
@CrossOrigin
@RestController
public class MyResourceUpdController extends YxyApiBaseController {

    @Autowired
    private YxyResourceService yrs;

    @ApiOperation(value = "老师编辑我的资源")
    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/tec/teaching/resource/myresource/upd", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        RespModel model = new RespModel();
        String userId = getProfile(request, response).getId();
        ElasticYxyResResource resource = yrs.findById(reqModel.id);
        Assert.notNull(resource, "资源未找到");
        Assert.isTrue(StringUtils.equals(resource.getUploaderId(), userId), "这不是您的资源");
        yrs.teaUpdResource(userId, reqModel.id, reqModel.typeId, reqModel.intro, reqModel.bookContent);
        EsResourceUpdateUtil.updateEsResoure(resource.getId());
        model.setMessage("成功");
        return model;
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String id;

        // 旧的资源目录
        public List<String> bookContent;

        // 类型
        public String typeId;

        // 简介
        public String intro;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(id, null), "找不到资源信息");
            Assert.notNull(StringUtils.defaultIfBlank(typeId, null), "找不到类型");
            Assert.isTrue(bookContent != null && bookContent.size() > 0, "请选择目录");
        }
    }

    protected static class RespModel extends YxyApiRespModel {

    }

}